package com.tfsswx.jcyw.cpureaper;

import java.util.Vector;

/**
 * @author 张述江
 * @date 2023/8/17
 */
public class MemTask implements Runnable {

    private final int num;
    private Vector v;

    public MemTask(int num) {
        this.num = num;
        v = new Vector();
    }

    @Override
    public void run() {
        //执行一个for循环进行新生代内存的申请，共消耗num数量GB
        for (int i = 0; i < num; i++) {
            byte[] b1 = new byte[1024 * 1024 * 1024]; //1G
            v.add(b1);
        }

        //内存消耗申请完后，执行死循环休眠，让JVM一直占用申请到的内存，达到一直占用num数量GB的效果
        while (true) {
            try {
                Thread.sleep(3600000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
